package org.cypress.rtpack.common.utils;

import org.springframework.context.MessageSource;

import java.util.Locale;

public class MessageUtils {

    private static MessageSource messageSource;

    public static String message(int code, Object... args) {
        return message(String.valueOf(code), args);
    }

    public static String message(String code, Object... args) {
        return message(code, null, args);
    }

    /**
     * 根据消息键和参数 获取消息
     * 委托给spring messageSource
     *
     * @param code   消息键
     * @param locale 国际化位置
     * @param args   参数
     * @return
     */
    public static String message(String code, Locale locale, Object... args) {
        if (messageSource == null) {
            messageSource = (MessageSource) SpringUtil.getBean("MessageSource");
        }
        return messageSource.getMessage(code, args, locale);
    }

}
